package com.kaka.bus.engine.home.info.card;

import com.kaka.bus.bean.CardBean;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.di.DataManager;
import com.kaka.bus.engine.common.impl.LoanService;
import com.kaka.bus.manager.Result;
import com.kaka.bus.manager.T;
import com.kaka.bus.mvp.RxPresenter;

import java.util.List;

import javax.inject.Inject;

public class CardListPresenter extends RxPresenter<CardListContract.View> implements CardListContract.Presenter {

    LoanService mLoanService;

    @Inject
    public CardListPresenter(DataManager dataManager, LoanService loanService) {
        super(dataManager);
        mLoanService = loanService;
        setService(mLoanService);
    }

    @Override
    public void initData(String userId, String token, int page, int pageSize) {
        request(userId,token,page,pageSize,false);
    }

    @Override
    public void loadMore(String userId, String token, int page, int pageSize) {
        request(userId,token,page,pageSize,true);
    }



    private void request(String userId, String token, int page, int pageSize, boolean isLoadMore) {
        addSubscribe(mDataManager.getApi()
                .getMyCards(userId, token, page, pageSize)
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<List<CardBean>>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<List<CardBean>> data) {
                        if (isLoadMore) {
                            mView.onResultLoadMore(data);
                        } else {
                            mView.onResultStart(data);
                        }
                    }

                    @Override
                    protected void onFaild() {
                        if(!isLoadMore)
                            mView.onError();
                    }
                }));
    }
}
